from django.shortcuts import *
from .models import *
# Create your views here.
def index(request):
    return render(request,'index.html')


def login(request):
    if request.method == 'GET':
        return render(request,'login.html')
    else:
        uname = request.POST.get('uname')
        pwd = request.POST.get('pwd')
        user = User.objects.filter(uname=uname,pwd=pwd).values('uname')
        if user:
           for i in user:
               d =  i['uname']
               request.session['user']=d

           return redirect(reverse('all'))


def all(request):
    if request.method == 'GET':
        all = Car_info.objects.all()
        return render(request, 'all.html',{'alls':all})


def add(request):
    if request.method == 'GET':
        all = Car_type.objects.all()
        return render(request, 'add.html',{'alls':all})
    iname = request.POST.get('iname')
    iP = request.POST.get('iP')
    iMark = request.POST.get('iMark')
    s = request.POST.get('s')
    new = Car_info(iname=iname,iPrice=iP,iMark=iMark,Car_type_id=s)
    new.save()
    return redirect(reverse('all'))


def delete(request,id):
    des = Car_info.objects.filter(pk=id)
    des.delete()
    return redirect(reverse('all'))


def update(request,id,sid):
    if request.method == 'GET':
        alls = Car_info.objects.filter(pk=id).first()
        all = Car_type.objects.all()
        return render(request, 'update.html',{'all':alls,'alls':all})
    else:
        iname = request.POST.get('iname')
        iP = request.POST.get('iP')
        iMark = request.POST.get('iMark')
        s = request.POST.get('s')
        Car_info.objects.filter(pk=id).update(iname=iname,iPrice=iP,iMark=iMark,Car_type_id=s)
        return redirect(reverse('all'))

